package com.gec.yhzh.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gec.yhzh.entity.AppointmentVaccine;
import com.gec.yhzh.service.IAppointmentVaccineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 可预约疫苗列表 前端控制器
 * </p>
 *
 * @author jerry
 * @since 2025-03-10
 */
@RestController
@RequestMapping("/appointmentVaccine")
public class AppointmentVaccineController {
    @Autowired
    private IAppointmentVaccineService iaappointmentVaccineService;

    // JSON解析工具
    ObjectMapper objectMapper = new ObjectMapper();

    @GetMapping("/query")
    public String query(@RequestParam(value = "vaccineId", required = false) String vaccineId,
                        @RequestParam(value = "address", required = false) String address) throws JsonProcessingException {
        Map<String, Object> result = new HashMap<>();
        try {
            System.out.println(address);
            List<AppointmentVaccine> appointmentVaccineList =
                    iaappointmentVaccineService.findListByParams(vaccineId, address);

            if (appointmentVaccineList == null || appointmentVaccineList.isEmpty()) {
                result.put("flag", false);
                result.put("message", "没有找到相关数据");
            } else {
                result.put("flag", true);
                result.put("message", "查询成功");
                result.put("list", appointmentVaccineList);
            }
        } catch (Exception e) {
            result.put("flag", false);
            result.put("message", "查询失败： " + e.getMessage());
        }
        return objectMapper.writeValueAsString(result);
    }
}